package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ModuleFileInfoPreview;
import com.ruoyi.system.service.IModuleFileInfoPreviewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件导入预览Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/system/filePreview")
public class ModuleFileInfoPreviewController extends BaseController {
    @Autowired
    private IModuleFileInfoPreviewService moduleFileInfoPreviewService;

    /**
     * 查询文件导入预览列表
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModuleFileInfoPreview moduleFileInfoPreview) {
        startPage();
        PageHelper.orderBy("efilenum");
        List<ModuleFileInfoPreview> list = moduleFileInfoPreviewService
                .selectModuleFileInfoPreviewList(moduleFileInfoPreview);
        return getDataTable(list);
    }

    /**
     * 获取二进制文件
     */
    @PostMapping("/getLocalFile")
    public ResponseEntity<byte[]> getLocalFile(@RequestBody ModuleFileInfoPreview moduleFileInfoPreview) {
        return moduleFileInfoPreviewService.getLocalFile(moduleFileInfoPreview.getId());
    }

    /**
     * 获取文件类型
     */
    @PostMapping("/getFileType")
    public String getType(@RequestBody ModuleFileInfoPreview moduleFileInfoPreview) {
        return moduleFileInfoPreviewService.getFileType(moduleFileInfoPreview.getId());
    }

    /**
     * 导出文件导入预览列表
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:export')")
    @Log(title = "文件导入预览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModuleFileInfoPreview moduleFileInfoPreview) {
        List<ModuleFileInfoPreview> list = moduleFileInfoPreviewService
                .selectModuleFileInfoPreviewList(moduleFileInfoPreview);
        ExcelUtil<ModuleFileInfoPreview> util = new ExcelUtil<ModuleFileInfoPreview>(ModuleFileInfoPreview.class);
        util.exportExcel(response, list, "文件导入预览数据");
    }

    /**
     * 获取文件导入预览详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(moduleFileInfoPreviewService.selectModuleFileInfoPreviewById(id));
    }

    /**
     * 新增文件导入预览
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:add')")
    @Log(title = "文件导入预览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModuleFileInfoPreview moduleFileInfoPreview) {
        return toAjax(moduleFileInfoPreviewService.insertModuleFileInfoPreview(moduleFileInfoPreview));
    }

    /**
     * 修改文件导入预览
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:edit')")
    @Log(title = "文件导入预览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModuleFileInfoPreview moduleFileInfoPreview) {
        return toAjax(moduleFileInfoPreviewService.updateModuleFileInfoPreview(moduleFileInfoPreview));
    }

    /**
     * 删除文件导入预览
     */
    // @PreAuthorize("@ss.hasPermi('system:preview:remove')")
    @Log(title = "文件导入预览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(moduleFileInfoPreviewService.deleteModuleFileInfoPreviewByIds(ids));
    }
}
